package com.coolingme.buffered;

import java.io.*;

/**
 * 缓冲字节型流 基础用法
 *
 * @author wangyue
 * @date 2019/4/3 10:03
 */
public class BufferedStreamTest {

    public static void main(String[] args) {
        File file = new File("D://Java//IntelliJIDEA//workspace//java-learn//IO//file//fileBuffered//bufferedStream.txt");//文件对象

        String path = "D://Java//IntelliJIDEA//workspace//java-learn//IO//file//fileBuffered//copy//bufferedStream.txt";//目标路径


        FileInputStream in = null;//字节型输入流

        FileOutputStream out = null;//字节型输出流

        BufferedInputStream bin = null;//缓冲输入流

        BufferedOutputStream bout = null;//缓冲输出流
        try {
            in = new FileInputStream(file);
            bin = new BufferedInputStream(in);//高级流需要低级流作为参数

            out = new FileOutputStream(path);
            bout = new BufferedOutputStream(out);//高级流需要低级流作为参数,没有第二个参数

            byte[] b = new byte[1024];//小推车
            int count = bin.read(b);//读取的字节数
            while (count != -1) {
                System.out.println(new String(b, 0, count));
                bout.write(b, 0, count);//写,需要设置读取的长度
                bout.flush();//刷新
                count = bin.read(b);
            }
            String string = "一二三四五六七八九十";
            bout.write(string.getBytes());//参数为字节数组
            bout.flush();//刷新
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭流
        }
    }
}
